package com.baizhi.service.imp;

import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Service
public class UserServiceImp implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public PageInfo<User> findAll(Integer page, Integer limit, User user) {
        PageHelper.startPage(page, limit);
        List<User> users = userDao.selectAll(user);
        return new PageInfo<>(users);
    }

    @Override
    public void addUserMultiple(List<User> users) {
        userDao.insertUserMultiple(users);
    }

    @Override
    public User findUserByFields(User user) {
        return userDao.selectUserByFields(user);
    }

    @Override
    public void addUser(User user) {
        userDao.insertUser(user);
    }

    @Override
    public void removeUserById(Integer userId) {
        userDao.deleteUserById(userId);
    }

    @Override
    public void removeMultiple(Integer[] ids) {
        userDao.deleteMultiple(ids);
    }

    @Override
    public void renovateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public Map findUserEcharts() throws InterruptedException {
        Map map = new HashMap<>();
        List<Map> maps01 = userDao.selectUserEchartsByRegister(0, 7);
        CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread(()->{
            map.put("one",userDao.selectUserEchartsByRegister(0,7));
            map.put("two",userDao.selectUserEchartsByRegister(8,14));
            map.put("three",userDao.selectUserEchartsByRegister(15,21));
            countDownLatch.countDown();
        }).start();
        new Thread(()->{
            map.put("province", userDao.selectUserEchartsByProvice());
            countDownLatch.countDown();
        }).start();

        new Thread(()->{
            map.put("sex",userDao.selectUserEchartsBySex());
            countDownLatch.countDown();
        }).start();

        countDownLatch.await();
        return map;
    }
}
